package com.poisonh.unifiedauth.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.dao.UserinfoDaoImpl;
import com.poisonh.unifiedauth.model.UserInfoModel;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.RegexUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

/**
 * 绑定用户信息
 * Created by PoisonH on 2016/6/28.
 */
public class SettingBindInfoActivity extends BaseActivity implements View.OnClickListener
{
    //姓名输入框
    private EditText mBindUserName;
    //身份证输入框
    private EditText mBindUserCard;
    //绑定身份按钮
    private Button mBtnBindInfo;
    //用户姓名
    private String mStrName;
    //身份证号
    private String mStrCard;
    //姓名至少2位
    private boolean name2bit = false;
    //身份证至少17位
    private boolean card18bit = false;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_settingbindinfo);
        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        mRightBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);
        mBindUserName = (EditText) findViewById(R.id.bind_input_username);
        mBindUserCard = (EditText) findViewById(R.id.bind_input_usercard);
        mBindUserName.addTextChangedListener(mNameTextWatcher);
        mBindUserCard.addTextChangedListener(mCardTextWatcher);
        mBtnBindInfo = (Button) findViewById(R.id.btn_bindinfo);
        mBtnBindInfo.setOnClickListener(this);
        setTitleTxt("身份信息");
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
            case R.id.btn_bindinfo:
                mStrName = mBindUserName.getText().toString();
                mStrCard = mBindUserCard.getText().toString();
                if (!RegexUtils.isIdCard(mStrCard))
                {
                    ToastUtils.showToast(SettingBindInfoActivity.this, "请输入正确的身份证号码");
                } else
                {
                    bindUserInfo();
                }
                break;
        }
    }

    /**
     * 绑定用户信息
     */
    private void bindUserInfo()
    {
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("realName", mStrName);
        params.put("card", mStrCard);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.MODIFYIDENTITY, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                ToastUtils.showToast(SettingBindInfoActivity.this, "更新用户信息成功");
                try
                {
                    //根据id查询用户
                    UserinfoDaoImpl mUserinfoDaoImpl = new UserinfoDaoImpl(SettingBindInfoActivity.this);
                    UserInfoModel model = mUserinfoDaoImpl.queryReturnOne(mStrUserId);
                    model.setmStrReallyName(mStrName);
                    model.setmStrCard(mStrCard);
                    mUserinfoDaoImpl.commit();
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
                startActivity(new Intent(SettingBindInfoActivity.this, FFmpegRecordActivity.class));
                finish();
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(SettingBindInfoActivity.this, "更新用户信息失败");
            }
        });
    }

    TextWatcher mNameTextWatcher = new TextWatcher()
    {
        private CharSequence temp;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if (temp.length() >= 2)
            {
                name2bit = true;
                changeBtnState();
            } else
            {
                name2bit = false;
                changeBtnState();
            }
        }
    };
    TextWatcher mCardTextWatcher = new TextWatcher()
    {
        private CharSequence temp;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if (temp.length() == 18)
            {
                card18bit = true;
                changeBtnState();
            } else
            {
                card18bit = false;
                changeBtnState();
            }
        }
    };

    /**
     * 根据输入动态改变按钮状态
     */
    private void changeBtnState()
    {
        if (name2bit && card18bit)
        {
            mBtnBindInfo.setEnabled(true);
            mBtnBindInfo.setBackground(getResources().getDrawable(R.drawable.shape_settingmyinfo_btn_bg));
        } else
        {
            mBtnBindInfo.setEnabled(false);
            mBtnBindInfo.setBackground(getResources().getDrawable(R.drawable.shape_settingmyinfo_btn_bg_));
        }
    }
}
